﻿using SiteUtils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.CMS
{
    public partial class ProductCate : System.Web.UI.Page
    {
        cmsProductCategoryBL proCateBL = new cmsProductCategoryBL();
        cmsProductBL proBL = new cmsProductBL();
        cmsProductCategoryDO objProCate = new cmsProductCategoryDO();
        private Cache cache = HttpContext.Current.Cache;
        private cmsProductBL productBL = new cmsProductBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            int productCategoryId = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                productCategoryId = int.Parse(Request.QueryString["ID"]);
                if (!IsPostBack)
                {
                    SeoWithId(productCategoryId);
                    ltrBreadcrumb.Text = Ultility.Breadcrumb("productcategory", productCategoryId);
                }
                rptHomeCategoriesDataSource(productCategoryId);
            }
            else
            {
                if (!IsPostBack)
                {
                    ltrBreadcrumb.Text = Ultility.Breadcrumb("productcategory", -1);
                    SeoWithoutId();
                    rptHomeCategoriesDataSource(productCategoryId);
                }
            }
        }
        protected void rptHomeCategoriesDataSource(int productCategoryId)
        {
            if (productCategoryId <= 0)
            {
                rptHomeCategories.DataSource = proCateBL.SelectAll();
            }
            else
            {
                cmsProductCategoryDO objCate = proCateBL.Select(new cmsProductCategoryDO { ProductCategoryID = productCategoryId });

                if (objCate.ParentID > 0)
                {
                    rptHomeCategories.DataSource = proCateBL.SelectById(productCategoryId);
                }
                else
                {
                    rptParentCategory.Visible = true;
                    rptParentCategory.DataSource = proCateBL.SelectById(productCategoryId);
                    rptParentCategory.DataBind();
                    rptHomeCategories.DataSource = proCateBL.SelectByParent(objCate.ProductCategoryID);
                }
            }
            rptHomeCategories.DataBind();
        }
        protected void rptParentCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string cacheName = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Repeater rptHomeProducts = (Repeater)e.Item.FindControl("rptHomeProducts");
                Panel pnlProductWrap = (Panel)e.Item.FindControl("pnlProductWrap");

                if (rptHomeProducts != null)
                {
                    CollectionPager collectAllProducts = (CollectionPager)e.Item.FindControl("collectAllProducts");
                    int productCategoryId = int.Parse(drv["ProductCategoryId"].ToString());

                    if (collectAllProducts != null)
                        collectAllProducts.Visible = true;
                    DataTable dtSource = proBL.SelectFrontPage(0, GetLocationId(), productCategoryId, -1, -1, -1, -1, -1);
                    if (dtSource.Rows.Count > 0)
                    {
                        LoadProducts(dtSource, rptHomeProducts, collectAllProducts, 20);
                    }
                    else
                    {
                        ///   pnlProductWrap.Visible = false;
                    }
                }
            }
        }
        protected void rptHomeCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string cacheName = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Repeater rptHomeProducts = (Repeater)e.Item.FindControl("rptHomeProducts");
                Panel pnlProductWrap = (Panel)e.Item.FindControl("pnlProductWrap");

                if (rptHomeProducts != null)
                {
                    CollectionPager collectAllProducts = (CollectionPager)e.Item.FindControl("collectAllProducts");
                    int productCategoryId = int.Parse(drv["ProductCategoryId"].ToString());
                    int parentId = int.Parse(drv["ParentId"].ToString());

                    if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    {
                        if (collectAllProducts != null)
                            collectAllProducts.Visible = true;
                        DataTable dtSource = proBL.SelectFrontPage(0, GetLocationId(), productCategoryId, -1, -1, -1, -1, -1);
                        if (dtSource.Rows.Count > 0)
                        {
                            LoadProducts(dtSource, rptHomeProducts, collectAllProducts, 20);
                        }
                        else
                        {
                            ///   pnlProductWrap.Visible = false;
                        }
                    }
                    else
                    {
                        if (collectAllProducts != null)
                            collectAllProducts.Visible = false;
                        DataTable dtSource = productBL.SelectFrontPage(10, GetLocationId(), productCategoryId, -1, -1, -1, -1, -1);
                        if (dtSource.Rows.Count > 0)
                        {
                            cacheName = "AllCategory_" + productCategoryId;
                            if (cache[cacheName] == null)
                            {
                                DataTable dtCache = dtSource;
                                if (dtCache != null)
                                    cache.Insert(cacheName, dtCache, null, DateTime.Now.AddSeconds(150), TimeSpan.Zero);
                            }
                            rptHomeProducts.DataSource = (DataTable)cache[cacheName];
                            rptHomeProducts.DataBind();
                        }
                        else
                        {
                            pnlProductWrap.Visible = false;
                        }
                    }
                }
            }
        }
        protected void rptHomeProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
            }
        }
        protected void SeoWithoutId()
        {
            //SEO
            this.Title = " Danh sách sản phẩm | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);

            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }
        protected void SeoWithId(int productCategoryId)
        {
            objProCate.ProductCategoryID = productCategoryId;
            proCateBL.Select(objProCate);

            this.Title = objProCate.ProductCategoryTitle + " | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);

            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objProCate.MetaDes;
            Page.Header.Controls.Add(metaDesc);
        }
        protected int GetLocationId()
        {
            int locationId = 0;
            if (Session["LocationId"] == null)
            {
                locationId = 0;
            }
            else
            {
                locationId = int.Parse(Session["LocationId"].ToString());
            }
            return locationId;
        }
        private void LoadProducts(DataTable dt, Repeater rpt, SiteUtils.CollectionPager collect, int pagesize)
        {
            if (dt.Rows.Count > 0)
            {
                collect.MaxPages = 10000;

                collect.PageSize = pagesize; // số items hiển thị trên một trang

                collect.DataSource = new DataView(dt, "", Ultility.GetSort(""), DataViewRowState.CurrentRows);

                collect.BindToControl = rpt;
                rpt.DataSource = collect.DataSourcePaged;
                rpt.DataBind();
            }
            else { }
        }



        /*

        protected void LoadData(int productCate)
        {
            string sort = ddlSort.SelectedValue;
            LoadProducts(proBL.SelectFrontPage(0, GetLocationId(), productCate, -1, -1, -1, -1,-1), rptAllProduct, collectAllProducts, 12, sort);
            LoadProducts(proBL.SelectFrontPage(0, GetLocationId(), productCate, -1, 1, -1, -1,-1), rptNewProducts, collectAllProducts, 12, sort);
            LoadProducts(proBL.SelectFrontPage(0, GetLocationId(), productCate, -1, -1, 1, -1,-1), rptSaleProducts, collectAllProducts, 12, sort);
        }
       
         */
    }
}